﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookStore.Solution.BLL;
using BookStore.Solution.Entities;
using BookStore.Solution.Utilities;

namespace BookStore.Solution.Presentation
{
    public partial class ucSupplier : UserControl
    {
        SupplierManager manager = new SupplierManager();
        DataTable MySupplierTable = null;

        private object[] CreateRow(Supplier entity)
        {
            return new object[] { entity.SupplierID, entity.SupplierName, entity.Address, entity.Email, entity.PhoneNumber, entity.Website };
        }

        void LoadData(DataTable myData)
        {
            dgvListofSupplier.DataSource = myData;
            for (int i = 0; i < dgvListofSupplier.Columns.Count; i++)
            {
                dgvListofSupplier.Columns[i].DataPropertyName = dgvListofSupplier.Columns[i].Name;
            }
        }
        Supplier GetRow()
        {
            Supplier supplier = new Supplier();
            supplier.SupplierID = DataUtil.ToString(dgvListofSupplier.SelectedRows[0].Cells[0].Value);
            supplier.SupplierName = DataUtil.ToString(dgvListofSupplier.SelectedRows[0].Cells[1].Value);
            supplier.Address = DataUtil.ToString(dgvListofSupplier.SelectedRows[0].Cells[2].Value);
            supplier.Email = DataUtil.ToString(dgvListofSupplier.SelectedRows[0].Cells[3].Value);
            supplier.PhoneNumber = DataUtil.ToString(dgvListofSupplier.SelectedRows[0].Cells[4].Value);
            supplier.Website = DataUtil.ToString(dgvListofSupplier.SelectedRows[0].Cells[5].Value);
            return supplier;
        }
        public ucSupplier()
        {
            InitializeComponent();
            dgvListofSupplier.AutoGenerateColumns = false;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmSupplier frm = new frmSupplier();
            frm.ShowDialog();
            if (frm.NewSupllier != null)
            {
                MySupplierTable.LoadDataRow(CreateRow(frm.NewSupllier), false);
                LoadData(MySupplierTable);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dgvListofSupplier.SelectedRows.Count > 0)
            {
                frmSupplier frm = new frmSupplier(GetRow());
                frm.ShowDialog();
                if (frm.NewSupllier != null)
                {
                    MySupplierTable.Rows[dgvListofSupplier.SelectedRows[0].Index].ItemArray = CreateRow(frm.NewSupllier);
                    LoadData(MySupplierTable);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvListofSupplier.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("Bạn có chắc chắn xóa không", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    Supplier entity = GetRow();
                    string result = manager.Delete(entity);
                    if (result == "Xóa thành công")
                    {
                        MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        MySupplierTable.Rows.RemoveAt(dgvListofSupplier.SelectedRows[0].Index);
                        LoadData(MySupplierTable);
                    }
                    else
                        MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            btnSearch.Expanded = true;
        }

        private void biSearchByID_Click(object sender, EventArgs e)
        {
            frmSearchSupplier frm = new frmSearchSupplier(SearchSupplierType.ID);
            frm.ShowDialog();
            if (frm.ResultTable != null)
            {
                LoadData(frm.ResultTable);
            }
        }

        private void biSearchByName_Click(object sender, EventArgs e)
        {
            frmSearchSupplier frm = new frmSearchSupplier(SearchSupplierType.Name);
            frm.ShowDialog();
            if (frm.ResultTable != null)
            {
                LoadData(frm.ResultTable);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            MySupplierTable = manager.GetAll();
            LoadData(MySupplierTable);
        }

        private bool SetRole()
        {
            bool t = false;
            List<string> roles = AccountManager.GetRoleName();
            btnAdd.Enabled = btnDelete.Enabled = btnUpdate.Enabled = false;
            btnSearch.Enabled = false;
            if (roles.Contains("Thêm, xóa, cập nhật nhà cung cấp"))
            {
                btnAdd.Enabled = btnDelete.Enabled = btnUpdate.Enabled = true;
                t = true;
            }
            if (roles.Contains("Tìm kiếm nhà cung cấp"))
            {
                btnSearch.Enabled = true;
                t = true;
            }
            return t;
        }

        private void ucSupplier_Load(object sender, EventArgs e)
        {
            if (SetRole())
            {
                MySupplierTable = manager.GetAll();
                LoadData(MySupplierTable);
            }
        }
    }
}
